<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码" show-password></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="手机号码" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号码"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="avatar">
      <single-upload v-model="dataForm.avatar"></single-upload>
    </el-form-item>
    <el-form-item label="是否禁用" prop="isDisable">
      <template slot-scope="scope">
        <el-switch
          v-model="dataForm.isDisable"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value=1
          :inactive-value=0
        ></el-switch>
      </template>
    </el-form-item>
      <el-form-item label="社交登录id" prop="socialUid">
        <el-input v-model="dataForm.socialUid" placeholder="社交登录id" disabled></el-input>
      </el-form-item>
    <el-form-item label="用户登录ip" prop="ipAddress">
      <el-input v-model="dataForm.ipAddress" placeholder="用户登录ip" disabled></el-input>
    </el-form-item>
    <el-form-item label="ip来源" prop="ipSource">
      <el-input v-model="dataForm.ipSource" placeholder="ip来源" disabled></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import singleUpload from '@/components/upload/singleUpload'
  export default {
    components: {singleUpload},
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          username: '',
          password: '',
          nickname: '',
          mobile: '',
          email: '',
          avatar: '',
          isDisable: '',
          loginType: '',
          ipAddress: '',
          ipSource: '',
          createTime: '',
          updateTime: '',
          socialUid: ''
        },
        dataRule: {
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          isDisable: [
            { required: false, message: '是否禁用', trigger: 'blur' }
          ],
          loginType: [
            { required: false, message: '登录类型', trigger: 'blur' }
          ],
          socialUid: [
            { required: false, message: '社交登录id', trigger: 'blur' }
          ],
          ipAddress: [
            { required: false, message: '用户登录ip不能为空', trigger: 'blur' }
          ],
          ipSource: [
            { required: false, message: 'ip来源不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornApi(`/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.user.username
                this.dataForm.password = data.user.password
                this.dataForm.nickname = data.user.nickname
                this.dataForm.mobile = data.user.mobile
                this.dataForm.email = data.user.email
                this.dataForm.avatar = data.user.avatar
                this.dataForm.isDisable = data.user.isDisable
                this.dataForm.loginType = data.user.loginType
                this.dataForm.socialUid = data.user.socialUid
                this.dataForm.ipAddress = data.user.ipAddress
                this.dataForm.ipSource = data.user.ipSource
                this.dataForm.createTime = data.user.createTime
                this.dataForm.updateTime = data.user.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornApi(`/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'nickname': this.dataForm.nickname,
                'mobile': this.dataForm.mobile,
                'email': this.dataForm.email,
                'avatar': this.dataForm.avatar,
                'isDisable': this.dataForm.isDisable,
                'loginType': this.dataForm.loginType,
                'socialUid': this.dataForm.socialUid,
                'ipAddress': this.dataForm.ipAddress,
                'ipSource': this.dataForm.ipSource,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
